/*
 * =====================================================================================
 *
 *       Filename:  pcap_loop.c
 *
 *    Description:  pcap_loop
 *
 *        Version:  1.0
 *        Created:  2012年11月29日 13时31分17秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <pcap/pcap.h>

void callback(u_char *user, const struct pcap_pkthdr *h,const u_char *bytes)
{
	printf("Get a new packet\n");
}

int main(int argc,char *argv[])
{
	if(argc<2)
	{
		fprintf(stderr,"You must specify the device you wanto sniff\n\n");
		return 1;
	}

	char *dev=argv[1];
	printf("Prepare sniff device %s ...\n",dev);

	char errbuf[PCAP_ERRBUF_SIZE];
	pcap_t *handle;

	handle=pcap_open_live(dev,BUFSIZ,1,1000,errbuf);
	if(handle == NULL)
	{
		fprintf(stderr,"Open device %s failed:%s\n",dev,errbuf);
		return 1;
	}

	pcap_loop(handle,-1,callback,NULL);
	pcap_close(handle);
	return 0;
}

